package moltencore.network.protocol;

import spark.network.protocol.Base;
import spark.network.Util;
import moltencore.game.Cannon;

public class CannonRotateStart extends Base {
	public static final short TYPE = 0x49;
	private static final byte[] TYPE_BYTES = Util.makeBytes(TYPE);

	private byte direction;
	private static final int DIRECTION_OFFSET = Base.SIZE;
	private static final int DIRECTION_LENGTH = Byte.SIZE / 8;

	protected static final int SIZE =
			Base.SIZE +
	        DIRECTION_LENGTH;

	public CannonRotateStart() {
		super(true, TYPE_BYTES);
	}

	/**
	 * Retrieves the direction in which to pivot the cannon.
	 * @return {@link moltencore.game.Cannon.Direction} if a direction was set. null otherwise.
	 */
	public Cannon.Direction getDirection() {
		switch(direction) {
			case -1:
				return Cannon.Direction.Left;
			case 1:
				return Cannon.Direction.Right;
			default:
				return null;
		}
	}

	public void setDirection(Cannon.Direction direction) {
		this.direction = (byte)((direction == Cannon.Direction.Left) ? -1 : 1);
	}

	@Override
	public void getBytes(byte[] buffer) {
		super.getBytes(buffer);
		buffer[DIRECTION_OFFSET] = direction;
	}

	@Override
	public void fill(byte[] buffer) {
		super.fill(buffer);
		direction = buffer[DIRECTION_OFFSET];
	}

	@Override
	public int getSize() {
		return SIZE;
	}

	public static CannonRotateStart create(byte[] buffer, long receiveTime) {
		CannonRotateStart packet = new CannonRotateStart();
		packet.fill(buffer);
		packet.setTimeStampReceive(receiveTime);
		return packet;
	}
}
